package com.wjy.Data_Structure.graph;

import com.wjy.Data_Structure.linearlist.common.Node;

//双链式存储结构的边定义
public class Edge {
	public static final int NORMAL = 0;
	public static final int MST = 1; //MST边 无向图小生成树的边
	public static final int CRITICAL = 2;//有向无环图关键路径中的边
	private int weight; //权值
	private Object info; //边的信息
	private Node edgePosition; //边在边表中的位置
	private Node firstVexPosition; //边的第一顶点与第二顶点
	private Node secondVexPosition;//在顶点表中的位置 
	private Node edgeFirstPosition;//边在第一(二)顶点的邻接(逆邻接)边表中的位置 
	private Node edgeSecondPosition;//在无向图中就是在两个顶点的邻接边表中的位置 
	private int type;//边的类型
	private int graphType;//所在图的类型 

	public Edge(Graph g, Vertex u, Vertex v, Object info) {
		this(g, u, v, info, 1);
	}

	public Edge(Graph g, Vertex u, Vertex v, Object info, int weight) {
		this.info = info;
		this.weight = weight;
		this.edgePosition = g.insert(this);
		this.firstVexPosition = u.getVexPosition();
		this.secondVexPosition = v.getVexPosition();
		this.type = Edge.NORMAL;
		this.graphType = g.getType();
		if (this.graphType == Graph.UndirectedGraph) {
			//如果是无向图,边应当加入其两个顶点的邻接边表 
			this.edgeFirstPosition = u.getAdjacentEdges().insertLast(this);
			this.edgeSecondPosition = v.getAdjacentEdges().insertLast(this);
		} else {
			//如果是有向图,边加入起始点的邻接边表,终止点的逆邻接边表 
			this.edgeFirstPosition = u.getAdjacentEdges().insertLast(this);
			this.edgeSecondPosition = v.getReAdjacentEdges().insertLast(this);
		}
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	public Object getInfo() {
		return info;
	}

	public void setInfo(Object info) {
		this.info = info;
	}

	public Node getEdgePosition() {
		return edgePosition;
	}

	public void setEdgePosition(Node edgePosition) {
		this.edgePosition = edgePosition;
	}

	public Node getFirstVexPosition() {
		return firstVexPosition;
	}

	public void setFirstVexPosition(Node firstVexPosition) {
		this.firstVexPosition = firstVexPosition;
	}

	public Node getSecondVexPosition() {
		return secondVexPosition;
	}

	public void setSecondVexPosition(Node secondVexPosition) {
		this.secondVexPosition = secondVexPosition;
	}

	public Node getEdgeFirstPosition() {
		return edgeFirstPosition;
	}

	public void setEdgeFirstPosition(Node edgeFirstPosition) {
		this.edgeFirstPosition = edgeFirstPosition;
	}

	public Node getEdgeSecondPosition() {
		return edgeSecondPosition;
	}

	public void setEdgeSecondPosition(Node edgeSecondPosition) {
		this.edgeSecondPosition = edgeSecondPosition;
	}

	public Vertex getFirstVex() {
		return (Vertex) this.firstVexPosition.getData();
	}

	public Vertex getSecondVex() {
		return (Vertex) this.secondVexPosition.getData();
	}

	//与边的类型相关的方法 
	public void setToMST() {
		this.type = Edge.MST;
	}

	public void setToCritical() {
		this.type = Edge.NORMAL;
	}

	public void resetType() {
		this.type = Edge.NORMAL;
	}

	public boolean isMSTEdge() {
		return type == Edge.MST;
	}

	public boolean isCritical() {
		return type == Edge.CRITICAL;
	}
}
